package com.jamal2362.apktool.util;

import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import com.jamal2362.apktool.R;

public class MDialog {
	private Context context;
	private AlertDialog.Builder builder;
	private DialogInterface.OnClickListener positiveClick;
	private DialogInterface.OnClickListener negativeClick;
	private DialogInterface.OnClickListener neutralClick;
	private String positiveText;
	private String negativeText;
	private String neutralText;

	public MDialog(Context ctx, String message) {
		View dialogView = LayoutInflater.from(ctx).inflate(R.layout.alert, null);
		TextView text = dialogView.findViewById(R.id.alertTextView1);
		text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Settings.appFontSize);
		text.setMovementMethod(LinkMovementMethod.getInstance());
		text.setText(Html.fromHtml(message));
		context = ctx;
		builder = new AlertDialog.Builder(ctx);
		builder.setView(dialogView);
	}

	public MDialog title(String str) {
		builder.setTitle(str);
		return this;
	}

	public MDialog cancelable(boolean val) {
		builder.setCancelable(val);
		return this;
	}

	public MDialog positive(int res) {
		return positive(context.getString(res), null);
	}

	public MDialog positive(String text) {
		return positive(text, null);
	}

	public MDialog positive(int res, final Runnable click) {
		return positive(context.getString(res), click);
	}

	public MDialog positive(String text, final Runnable click) {
		positiveText = text;
		if (click instanceof Runnable) {
			positiveClick = new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface p1, int p2) {
					click.run();
					p1.dismiss();
				}
			};
		}
		builder.setPositiveButton(positiveText, positiveClick);
		return this;
	}

	public MDialog negative(int res) {
		return negative(context.getString(res), null);
	}

	public MDialog negative(String text) {
		return negative(text, null);
	}

	public MDialog negative(int res, final Runnable click) {
		return negative(context.getString(res), click);
	}

	public MDialog negative(String text, final Runnable click) {
		negativeText = text;
		if (click instanceof Runnable) {
			negativeClick = new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface p1, int p2) {
					click.run();
					p1.dismiss();
				}
			};
		}
		builder.setNegativeButton(negativeText, negativeClick);
		return this;
	}

	public MDialog neutral(int res) {
		return neutral(context.getString(res), null);
	}

	public MDialog neutral(String text) {
		return neutral(text, null);
	}

	public MDialog neutral(int res, final Runnable click) {
		return neutral(context.getString(res), click);
	}

	public MDialog neutral(String text, final Runnable click) {
		neutralText = text;
		if (click instanceof Runnable) {
			neutralClick = new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface p1, int p2) {
					click.run();
					p1.dismiss();
				}
			};
		}
		builder.setNeutralButton(neutralText, neutralClick);
		return this;
	}

	public void show() {
		builder.create().show();
	}
}
